let username = localStorage.getItem('username') || '张三';
let bannerHtml = `<h3 id="logo"><a href="/jobList.html">拉勾招聘</a></h3>
    <div id="userBox"><a href="/uploadCV.html">上传简历</a>&nbsp;&nbsp;&nbsp;<span>${username}<span></div>
    <div class="clear"></div>`;
$("#banner").html(bannerHtml);
$("#banner").css('text-align', 'right');
$("#banner").css('padding', '15px 50px 50px');
$("#banner #logo").css('float', 'left');
//$("#banner #logo").css('border', '1px solid black');
$("#banner #logo").css('padding', '30px');
$("#banner #logo").css('font-size', '25px');
$("#banner #logo").css('margin', '0');
$("#banner #logo").css('position', 'relative');
$("#banner #logo").css('top', '-20px');
$("#banner #logo").css('left', '-50px');

$(".clear").css("clear", "both");

Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}



function opLog(opType, data) {

    data ={...data, opTime: (new Date()).Format("yyyy-MM-dd hh:mm:ss"), action: opType}

    // 记录点击链接操作日志
    $.ajax({
        type: "POST",
        url: `http://logs.lagou-demo.com:61066/api/log/${opType}`,
        dataType: 'json',
        contentType: 'application/json',
        async: false,
        data: typeof(data) === typeof({}) ? JSON.stringify(data) : data
    })
}


// 超链接拦截埋点
function interceptClickEvent(e) {
    var href;
    var target = e.target || e.srcElement;
    if (target.tagName === 'A') {
        href = target.getAttribute('href');

        console.log(`${username} 点击链接 ${href}`);

        // 记录点击链接操作日志
        opLog('click', { "userId": username, "url" : href });
//        e.preventDefault();
    }
}

if (document.addEventListener) {
    document.addEventListener('click', interceptClickEvent);
} else if (document.attachEvent) {
    document.attachEvent('onclick', interceptClickEvent);
}


// ajax请求进行拦截埋点
XMLHttpRequest.prototype.open = (function(open) {
  return function(method, url, async) {
      console.log(`${username} ajax访问 ${url}`);
      logXhrOperations(url);

      open.apply(this,arguments);
    };
})(XMLHttpRequest.prototype.open);

function logXhrOperations(url) {
    let urlObj = new URL(url, baseUrl() + '/');

    console.log(urlObj);
    console.log(urlObj.href);
    console.log(urlObj.pathname);
    let jobId, cvId;

    switch(urlObj.pathname) {
        case '/api/jobCollect':
            jobId = urlObj.search.split('jobId=')[1].split('&')[0];
            opLog('job_collect', {userId: username, jobId:jobId });
            break;

        case '/api/sendCV':
            jobId = urlObj.search.split('jobId=')[1].split('&')[0];
            opLog('cv_send', {userId: username, jobId:jobId, cvId:"12345" });
            break;

        case '/api/uploadCV':
            opLog('cv_upload', {userId: username,  cvId:"12345" });
            break;
    }

}

function baseUrl() {
    let currPath = window.location.href;
    currPath = currPath.split('?')[0];
    let lastSlashPos = currPath.lastIndexOf('/');
    if (lastSlashPos > 6) {
        return currPath.substring(0, lastSlashPos);
    } else {
        return lastSlashPos;
    }

}